package Servelt;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String email=request.getParameter("email");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String gender=request.getParameter("gender");
        String telephone=request.getParameter("telephone");
        String introduce=request.getParameter("introduce");
        gender=new String(gender.getBytes("iso-8859-1"),"utf-8");
        introduce=new String(introduce.getBytes("iso-8859-1"),"utf-8");
        System.out.println(gender+introduce);

        Connection conn = null;   PreparedStatement preparedStatement=null;   ResultSet rs;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("加载驱动成功！");

            conn= DriverManager.getConnection("jdbc:mysql://localhost:3307/xiangmu?characterEncoding=utf8","root","1234");
            String sql = "insert into vip(email,username,password,gender,telephone,introduce)"+"values(?,?,?,?,?,?)";
            preparedStatement = conn.prepareStatement(sql);

          preparedStatement.setString(1,email);
          preparedStatement.setString(2,username);
            preparedStatement.setString(3,password);
            preparedStatement.setString(4,gender);
            preparedStatement.setString(5,telephone);
            preparedStatement.setString(6,introduce);

          preparedStatement.executeUpdate();

           preparedStatement.close();	conn.close();
           response.getWriter().println("注册成功！");
        } catch (Exception e)
        {
            System.out.println(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
       resp.getWriter().println("这是get请求");
    }
}
